<?php


use Phinx\Migration\AbstractMigration;

class FillDefaultStepData extends AbstractMigration
{

    /**
     * 模块
     * @var array
     */
    protected $moduleDictData = [];

    protected $stepTable;

    protected $stepCategoryTable;


    public function randColor()
    {
        $colors = array();
        for ($i = 0; $i < 6; $i++) {
            $colors[] = dechex(rand(0, 15));
        }
        return '#' . implode('', $colors);
    }


    /**
     * 获取所有模块数据
     * @return array
     */
    public function getAllModuleData()
    {
        $sql = 'select * from module';
        $result = $this->query($sql);
        $info = array();
        foreach ($result as $key => $val) {
            $info[$val['code']] = $val;
        }
        return $info;
    }

    /**
     * 递归插入工序相关数据
     * @param $stepCategory
     * @param int $stepCategoryId
     * @param bool $isStep
     * @throws Exception
     */
    public function recursionAddStepData(&$stepCategory, $stepCategoryId = 0, $isStep = false)
    {
        // 递归插入
        foreach ($stepCategory as $stepItem) {
            if ($isStep) {
                // 添加工序
                $stepItem['step_category_id'] = $stepCategoryId;
                $stepItem['color'] = $this->randColor();
                $stepItem['uuid'] = Webpatser\Uuid\Uuid::generate()->string;
                $this->stepTable->insert($stepItem)->save();

            } else {
                // 添加工序分类
                if (array_key_exists('data', $stepItem)) {
                    $stepCategoryItem = $stepItem['data'];
                    $stepCategoryItem['parent_id'] = $stepCategoryId;
                    $stepCategoryItem['entity_id'] = !empty($this->moduleDictData[$stepCategoryItem['code']]) ? $this->moduleDictData[$stepCategoryItem['code']]['id'] : 0;
                    $stepCategoryItem['uuid'] = Webpatser\Uuid\Uuid::generate()->string;

                    $this->stepCategoryTable->insert($stepCategoryItem)->save();
                    $query = $this->fetchRow('SELECT max(`id`) as id FROM step_category');

                    if (array_key_exists('child', $stepItem)) {
                        $this->recursionAddStepData($stepItem['child'], $query['id'], false);
                    } else if (array_key_exists('step', $stepItem)) {
                        $this->recursionAddStepData($stepItem['step'], $query['id'], true);
                    }
                }
            }
        }
    }

    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex13
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */

    /**
     * Migrate Up.
     * @throws Exception
     */
    public function up()
    {
        $this->moduleDictData = $this->getAllModuleData();

        $stepCategory = [
            [
                "data" => [
                    "code" => "creative",
                    "name" => "创作"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#a8a4ff",
                                "code" => "story_worldview",
                                "name" => "世界观",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "story_worldview",
                            "name" => "世界观"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#8387d5",
                                "code" => "character_list",
                                "name" => "人物清单",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "level_design",
                            "name" => "人物清单"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#00D6B2",
                                "code" => "props_list",
                                "name" => "道具清单",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "props_list",
                            "name" => "道具清单"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#08B8EE",
                                "code" => "scene_level_list",
                                "name" => "场景关卡清单",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "scene_level_list",
                            "name" => "场景关卡清单"
                        ]
                    ]
                ]
            ], // 创作
            [
                "data" => [
                    "code" => "script",
                    "name" => "剧本"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#c8bb42",
                                "code" => "story_outline",
                                "name" => "剧本大纲",
                                "variable" => 1,
                            ],
                            [
                                "color" => "#11ae09",
                                "code" => "polt_outline",
                                "name" => "剧情梗概",
                                "variable" => 1,
                            ], [
                                "color" => "#69477d",
                                "code" => "story",
                                "name" => "剧本",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "episodes",
                            "name" => "集数"
                        ]
                    ]
                ]
            ], // 剧本
            [
                "data" => [
                    "code" => "design",
                    "name" => "概念"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#efd3f4",
                                "code" => "sketch_design",
                                "name" => "草图设计",
                                "variable" => 32,
                            ],
                            [
                                "color" => "#6b0c60",
                                "code" => "color_draft_design",
                                "name" => "色稿设计",
                                "variable" => 48,
                            ],
                            [
                                "color" => "#e52350",
                                "code" => "post_design",
                                "name" => "细化设计",
                                "variable" => 16,
                            ],
                            [
                                "color" => "#0DDFFF",
                                "code" => "split_design",
                                "name" => "拆分设计",
                                "variable" => 16,
                            ],
                            [
                                "color" => "#9957cb",
                                "code" => "face_expression_design",
                                "name" => "表情设计",
                                "variable" => 24,
                            ]
                        ],
                        "data" => [
                            "code" => "design_entity",
                            "name" => "概念"
                        ]
                    ]
                ]
            ], // 概念
            [
                "data" => [
                    "code" => "storyboard",
                    "name" => "分镜"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#F200FF",
                                "code" => "script",
                                "name" => "脚本",
                                "variable" => 1,
                            ],
                            [
                                "color" => "#FF0022",
                                "code" => "storyboard",
                                "name" => "分镜",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "storyboard_entity",
                            "name" => "分镜"
                        ]
                    ]
                ]
            ], // 分镜
            [
                "data" => [
                    "code" => "asset",
                    "name" => "资产"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#45c666",
                                "code" => "high_model",
                                "name" => "雕刻模型",
                                "variable" => 144,
                            ],
                            [
                                "color" => "#d7855b",
                                "code" => "engine_model",
                                "name" => "引擎模型",
                                "variable" => 28,
                            ],
                            [
                                "color" => "#130419",
                                "code" => "amin_model",
                                "name" => "动画模型",
                                "variable" => 16,
                            ],
                            [
                                "color" => "#b0bb22",
                                "code" => "texture",
                                "name" => "贴图材质",
                                "variable" => 64,
                            ],
                            [
                                "color" => "#977751",
                                "code" => "hair",
                                "name" => "毛发",
                                "variable" => 16,
                            ],
                            [
                                "color" => "#445adf",
                                "code" => "character_face_expression",
                                "name" => "表情模型",
                                "variable" => 24,
                            ],
                            [
                                "color" => "#33FF00",
                                "code" => "uv_baking",
                                "name" => "UV和烘焙",
                                "variable" => 24,
                            ],
                            [
                                "color" => "#0084FF",
                                "code" => "expression_normal",
                                "name" => "表情法线",
                                "variable" => 16,
                            ]
                        ],
                        "data" => [
                            "code" => "model",
                            "name" => "模型"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#6a5daa",
                                "code" => "body_rig",
                                "name" => "身体绑定",
                                "variable" => 80,
                            ],
                            [
                                "color" => "#0ddb5a",
                                "code" => "face_expression_rig",
                                "name" => "表情绑定",
                                "variable" => 4,
                            ],
                            [
                                "color" => "#889a21",
                                "code" => "cloth_rig",
                                "name" => "布料绑定",
                                "variable" => 24,
                            ],
                            [
                                "color" => "#5e4fed",
                                "code" => "prop_rig",
                                "name" => "道具绑定",
                                "variable" => 1,
                            ]
                        ],
                        "data" => [
                            "code" => "rig",
                            "name" => "绑定"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#ccc2d7",
                                "code" => "engine_material_set",
                                "name" => "引擎材质",
                                "variable" => 8,
                            ],
                            [
                                "color" => "#1f4172",
                                "code" => "engine_cloth_set",
                                "name" => "引擎布料",
                                "variable" => 8,
                            ]
                        ],
                        "data" => [
                            "code" => "engine_set",
                            "name" => "引擎设置"
                        ]
                    ]
                ]
            ], // 资产
            [
                "data" => [
                    "code" => "level",
                    "name" => "关卡"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#b0b928",
                                "code" => "engine_level",
                                "name" => "引擎关卡",
                                "variable" => 80,
                            ],
                            [
                                "color" => "#794253",
                                "code" => "engine_sub_level",
                                "name" => "细分关卡",
                                "variable" => 48,
                            ]
                        ],
                        "data" => [
                            "code" => "level_engine",
                            "name" => "关卡制作"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#b40401",
                                "code" => "level_engine_efx",
                                "name" => "关卡特效",
                                "variable" => 2,
                            ]
                        ],
                        "data" => [
                            "code" => "level_efx",
                            "name" => "关卡特效"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#3ebaa7",
                                "code" => "levle_engine_light",
                                "name" => "关卡灯光",
                                "variable" => 24,
                            ]
                        ],
                        "data" => [
                            "code" => "level_light",
                            "name" => "关卡灯光"
                        ]
                    ]
                ]
            ], // 关卡
            [
                "data" => [
                    "code" => "sequence",
                    "name" => "序列"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#17b1a9",
                                "code" => "motion_track",
                                "name" => "动作捕捉",
                                "variable" => 2,
                            ],
                            [
                                "color" => "#cdb0b3",
                                "code" => "face_track",
                                "name" => "表情捕捉",
                                "variable" => 2,
                            ]
                        ],
                        "data" => [
                            "code" => "mocap",
                            "name" => "捕捉"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#236313",
                                "code" => "layout",
                                "name" => "layout",
                                "variable" => 2,
                            ]
                        ],
                        "data" => [
                            "code" => "preview",
                            "name" => "预演"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#7E06D1",
                                "code" => "animation",
                                "name" => "动画",
                                "variable" => 2,
                            ]
                        ],
                        "data" => [
                            "code" => "motion",
                            "name" => "动作"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#c98b86",
                                "code" => "ex_cloth_solution",
                                "name" => "布料解算",
                                "variable" => 2,
                            ]
                        ],
                        "data" => [
                            "code" => "solution",
                            "name" => "解算"
                        ]
                    ]
                ]
            ], // 序列
            [
                "data" => [
                    "code" => "shot",
                    "name" => "镜头"
                ],
                "child" => [
                    [
                        "step" => [
                            [
                                "color" => "#26c265",
                                "code" => "engine_light",
                                "name" => "引擎灯光",
                                "variable" => 3,
                            ]
                        ],
                        "data" => [
                            "code" => "lighting",
                            "name" => "灯光"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#d10df8",
                                "code" => "engine_efx",
                                "name" => "引擎特效",
                                "variable" => 3,
                            ],
                            [
                                "color" => "#c2b2e8",
                                "code" => "other_efx",
                                "name" => "其他特效",
                                "variable" => 3,
                            ]
                        ],
                        "data" => [
                            "code" => "efx",
                            "name" => "特效"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#a55a21",
                                "code" => "engine_render",
                                "name" => "引擎渲染",
                                "variable" => 3,
                            ],
                            [
                                "color" => "#b08630",
                                "code" => "other_render",
                                "name" => "其他渲染",
                                "variable" => 3,
                            ]
                        ],
                        "data" => [
                            "code" => "Rendering",
                            "name" => "渲染"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#5772ba",
                                "code" => "comp",
                                "name" => "合成",
                                "variable" => 3,
                            ]
                        ],
                        "data" => [
                            "code" => "comp",
                            "name" => "合成"
                        ]
                    ],
                    [
                        "step" => [
                            [
                                "color" => "#4fb64b",
                                "code" => "color",
                                "name" => "调色",
                                "variable" => 3,
                            ]
                        ],
                        "data" => [
                            "code" => "color",
                            "name" => "调色"
                        ]
                    ]
                ]
            ] // 镜头
        ];

        $this->stepTable = $this->table('step');
        $this->stepCategoryTable = $this->table('step_category');

        $this->recursionAddStepData($stepCategory);
    }

    /**
     * Migrate Down.
     */
    function down()
    {
        $this->execute('DELETE FROM step');
        $this->execute('DELETE FROM step_category');
    }
}
